//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "CoterminalSwapCurveState.h"
using namespace Cephei::QL::Models::Marketmodels::Curvestates;
#include <gen/QL/Models/Marketmodels/CurveState.h>
using namespace Cephei::QL::Models::Marketmodels;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::Curvestates::CCoterminalSwapCurveState::CCoterminalSwapCurveState (Cephei::Core::IVector<Double>^ rateTimes) : CCurveState(CCoterminalSwapCurveState::typeid)
{
    try
    {
#ifdef HANDLE
        _phCoterminalSwapCurveState = NULL;
#endif
        rateTimes->Lock();
        INativeVector<Double>^ _NCIrateTimes = rateTimes->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCrateTimes = safe_cast<CDoubleVector^>(_NCIrateTimes);
        std::vector<QuantLib::Time>& _rateTimes = static_cast<std::vector<QuantLib::Time>&> (_NCrateTimes->GetReference ());
        _ppCoterminalSwapCurveState = new boost::shared_ptr<QuantLib::CoterminalSwapCurveState> (new QuantLib::CoterminalSwapCurveState ( _rateTimes ));
        SetCurveState (boost::dynamic_pointer_cast<QuantLib::CurveState> (*_ppCoterminalSwapCurveState));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (rateTimes != nullptr) rateTimes->Unlock();    //not optional
    }
}
Cephei::QL::Models::Marketmodels::Curvestates::CCoterminalSwapCurveState::CCoterminalSwapCurveState (boost::shared_ptr<QuantLib::CoterminalSwapCurveState>& childNative, Object^ owner) : CCurveState(CCoterminalSwapCurveState::typeid)
{
#ifdef HANDLE
	_phCoterminalSwapCurveState = NULL;
#endif
	_ppCoterminalSwapCurveState = &childNative;
    _ppCurveState = new boost::shared_ptr<QuantLib::CurveState> (boost::dynamic_pointer_cast<QuantLib::CurveState> (*_ppCoterminalSwapCurveState));
}
Cephei::QL::Models::Marketmodels::Curvestates::CCoterminalSwapCurveState::CCoterminalSwapCurveState (QuantLib::CoterminalSwapCurveState& childNative, Object^ owner) : CCurveState(CCoterminalSwapCurveState::typeid)
{
#ifdef HANDLE
	_phCoterminalSwapCurveState = NULL;
#endif
	_ppCoterminalSwapCurveState = new boost::shared_ptr<QuantLib::CoterminalSwapCurveState> (&childNative);
    _ppCurveState = new boost::shared_ptr<QuantLib::CurveState> (boost::dynamic_pointer_cast<QuantLib::CurveState> (*_ppCoterminalSwapCurveState));
    _CoterminalSwapCurveStateOwner = owner;
    _CurveStateOwner = owner;
}

Cephei::QL::Models::Marketmodels::Curvestates::CCoterminalSwapCurveState::CCoterminalSwapCurveState (CCoterminalSwapCurveState^ copy) : CCurveState(CCoterminalSwapCurveState::typeid)
{
#ifdef HANDLE
	_phCoterminalSwapCurveState = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppCoterminalSwapCurveState = new boost::shared_ptr<QuantLib::CoterminalSwapCurveState> (copy->GetShared());
        _ppCurveState = new boost::shared_ptr<QuantLib::CurveState> (boost::dynamic_pointer_cast<QuantLib::CurveState> (*_ppCoterminalSwapCurveState));
    }
}
Cephei::QL::Models::Marketmodels::Curvestates::CCoterminalSwapCurveState::CCoterminalSwapCurveState (PLATFORM::Type^ t) : CCurveState(CCoterminalSwapCurveState::typeid)
{
#ifdef HANDLE
	_phCoterminalSwapCurveState = NULL;
#endif
	if (!t->IsSubclassOf(CCoterminalSwapCurveState::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::Curvestates::CCoterminalSwapCurveState::CCoterminalSwapCurveState (QuantLib::Handle<QuantLib::CoterminalSwapCurveState>& childNative, Object^ owner)  : CCurveState(CCoterminalSwapCurveState::typeid)
{
	_phCoterminalSwapCurveState = &childNative;
	_ppCoterminalSwapCurveState = &static_cast<boost::shared_ptr<QuantLib::CoterminalSwapCurveState>>(childNative.currentLink());
    _ppCurveState = new boost::shared_ptr<QuantLib::CurveState> (boost::dynamic_pointer_cast<QuantLib::CurveState> (*_ppCoterminalSwapCurveState));
    _CoterminalSwapCurveStateOwner = owner;
}
Cephei::QL::Models::Marketmodels::Curvestates::CCoterminalSwapCurveState::CCoterminalSwapCurveState (QuantLib::Handle<QuantLib::CoterminalSwapCurveState> childNative)  : CCurveState(CCoterminalSwapCurveState::typeid)
{
	_phCoterminalSwapCurveState = &childNative;
	_ppCoterminalSwapCurveState = &static_cast<boost::shared_ptr<QuantLib::CoterminalSwapCurveState>>(childNative.currentLink());
    _ppCurveState = new boost::shared_ptr<QuantLib::CurveState> (boost::dynamic_pointer_cast<QuantLib::CurveState> (*_ppCoterminalSwapCurveState));
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::Curvestates::CCoterminalSwapCurveState::CCoterminalSwapCurveState (QuantLib::CoterminalSwapCurveState childNative)  : CCurveState(CCoterminalSwapCurveState::typeid)
{
#ifdef HANDLE
	_phCoterminalSwapCurveState = NULL;
#endif
	_ppCoterminalSwapCurveState = new boost::shared_ptr<QuantLib::CoterminalSwapCurveState> (new QuantLib::CoterminalSwapCurveState (childNative));
    _ppCurveState = new boost::shared_ptr<QuantLib::CurveState> (boost::dynamic_pointer_cast<QuantLib::CurveState> (*_ppCoterminalSwapCurveState));
}
#endif

Cephei::QL::Models::Marketmodels::Curvestates::CCoterminalSwapCurveState::~CCoterminalSwapCurveState ()
{
    if (_ppCoterminalSwapCurveState != NULL)
    {
	    delete _ppCoterminalSwapCurveState;
        _ppCoterminalSwapCurveState = NULL;
    }
}
Cephei::QL::Models::Marketmodels::Curvestates::CCoterminalSwapCurveState::!CCoterminalSwapCurveState ()
{
    if (_ppCoterminalSwapCurveState != NULL)
    {
	    delete _ppCoterminalSwapCurveState;
    }
}
QuantLib::CoterminalSwapCurveState& Cephei::QL::Models::Marketmodels::Curvestates::CCoterminalSwapCurveState::GetReference ()
{
    if (_ppCoterminalSwapCurveState == NULL) throw REFNEW NativeNullException ();
	return **_ppCoterminalSwapCurveState;
}
boost::shared_ptr<QuantLib::CoterminalSwapCurveState>& Cephei::QL::Models::Marketmodels::Curvestates::CCoterminalSwapCurveState::GetShared ()
{
    if (_ppCoterminalSwapCurveState == NULL) throw REFNEW NativeNullException ();
	return *_ppCoterminalSwapCurveState;
}
QuantLib::CoterminalSwapCurveState* Cephei::QL::Models::Marketmodels::Curvestates::CCoterminalSwapCurveState::GetPointer ()
{
    if (_ppCoterminalSwapCurveState == NULL) throw REFNEW NativeNullException ();
	return &**_ppCoterminalSwapCurveState;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::CoterminalSwapCurveState>& Cephei::QL::Models::Marketmodels::Curvestates::CCoterminalSwapCurveState::GetHandle ()
{
	if (_phCoterminalSwapCurveState == NULL)
	{
		_phCoterminalSwapCurveState = new Handle<QuantLib::CoterminalSwapCurveState> (*_ppCoterminalSwapCurveState);
	}
	return *_phCoterminalSwapCurveState;
}
#endif
bool Cephei::QL::Models::Marketmodels::Curvestates::CCoterminalSwapCurveState::HasNative () 
{
	return (_ppCoterminalSwapCurveState != NULL);
}

Double Cephei::QL::Models::Marketmodels::Curvestates::CCoterminalSwapCurveState::CmSwapAnnuity (UInt64 numeraire, UInt64 i, UInt64 spanningForwards)
{
    try
    {
        QuantLib::Size _numeraire = (QuantLib::Size)ValueHelper::Convert (numeraire); //a
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i); //a
        QuantLib::Size _spanningForwards = (QuantLib::Size)ValueHelper::Convert (spanningForwards); //a
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppCoterminalSwapCurveState)->cmSwapAnnuity ( _numeraire,  _i,  _spanningForwards );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Models::Marketmodels::Curvestates::CCoterminalSwapCurveState::CmSwapRate (UInt64 i, UInt64 spanningForwards)
{
    try
    {
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i); //a
        QuantLib::Size _spanningForwards = (QuantLib::Size)ValueHelper::Convert (spanningForwards); //a
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppCoterminalSwapCurveState)->cmSwapRate ( _i,  _spanningForwards );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Models::Marketmodels::Curvestates::CCoterminalSwapCurveState::CmSwapRates (UInt64 spanningForwards)
{
    try
    {
        QuantLib::Size _spanningForwards = (QuantLib::Size)ValueHelper::Convert (spanningForwards); //a
    	std::vector<QuantLib::Rate>& _rv = (std::vector<QuantLib::Rate>&)(*_ppCoterminalSwapCurveState)->cmSwapRates ( _spanningForwards );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Models::Marketmodels::Curvestates::CCoterminalSwapCurveState::CoterminalSwapAnnuity (UInt64 numeraire, UInt64 i)
{
    try
    {
        QuantLib::Size _numeraire = (QuantLib::Size)ValueHelper::Convert (numeraire); //a
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i); //a
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppCoterminalSwapCurveState)->coterminalSwapAnnuity ( _numeraire,  _i );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Models::Marketmodels::Curvestates::CCoterminalSwapCurveState::CoterminalSwapRate (UInt64 i)
{
    try
    {
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i); //a
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppCoterminalSwapCurveState)->coterminalSwapRate ( _i );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Models::Marketmodels::Curvestates::CCoterminalSwapCurveState::CoterminalSwapRates::get ()
{
    try
    {
    	std::vector<QuantLib::Rate>& _rv = (std::vector<QuantLib::Rate>&)(*_ppCoterminalSwapCurveState)->coterminalSwapRates ( );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Models::Marketmodels::Curvestates::CCoterminalSwapCurveState::DiscountRatio (UInt64 i, UInt64 j)
{
    try
    {
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i); //a
        QuantLib::Size _j = (QuantLib::Size)ValueHelper::Convert (j); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppCoterminalSwapCurveState)->discountRatio ( _i,  _j );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Models::Marketmodels::Curvestates::CCoterminalSwapCurveState::ForwardRate (UInt64 i)
{
    try
    {
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i); //a
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppCoterminalSwapCurveState)->forwardRate ( _i );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Models::Marketmodels::Curvestates::CCoterminalSwapCurveState::ForwardRates::get ()
{
    try
    {
    	std::vector<QuantLib::Rate>& _rv = (std::vector<QuantLib::Rate>&)(*_ppCoterminalSwapCurveState)->forwardRates ( );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Models::Marketmodels::Curvestates::ICoterminalSwapCurveState^ Cephei::QL::Models::Marketmodels::Curvestates::CCoterminalSwapCurveState::SetOnCoterminalSwapRates (Cephei::Core::IVector<Double>^ swapRates, Microsoft::FSharp::Core::FSharpOption<UInt64>^ firstValidIndex)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        swapRates->Lock();
        INativeVector<Double>^ _NCIswapRates = swapRates->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCswapRates = safe_cast<CDoubleVector^>(_NCIswapRates);
        std::vector<QuantLib::Rate>& _swapRates = static_cast<std::vector<QuantLib::Rate>&> (_NCswapRates->GetReference ());
        QuantLib::Size _firstValidIndex = 
            (Microsoft::FSharp::Core::FSharpOption<UInt64>::IsSome::get (firstValidIndex) ? (QuantLib::Size)ValueHelper::Convert (firstValidIndex->Value) : 0); //9a
    	(*_ppCoterminalSwapCurveState)->setOnCoterminalSwapRates ( _swapRates,  _firstValidIndex );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (swapRates != nullptr) swapRates->Unlock(); //NO2  
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Models::Marketmodels::Curvestates::ICoterminalSwapCurveState^ Cephei::QL::Models::Marketmodels::Curvestates::CCoterminalSwapCurveState_Factory::Create (Cephei::Core::IVector<Double>^ rateTimes)
{
    return REFNEW CCoterminalSwapCurveState ( rateTimes);
}
